﻿<%@ WebHandler Language="C#" Class="AHMHRIRSL01" %>

using System;
using System.Web;
using System.Linq;
using System.Collections.Generic;
using System.Configuration;

public class AHMHRIRSL01 : IHttpHandler
{
    string vappid = "AHMIC";
    string vformid = "AHMIC_AHMHRIRSL01";

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "application/json";
        AjaxResponse axresp = new AjaxResponse();
        string user = AHMIDEAFunctions.ContextUser(context);

        if (AHMIDEAFunctions.IsFunctionAuthorized(context, vappid, vformid, "OPEN"))
        {
            // Field Names
            string f_filter_nrp = "filter_nrp";
            string f_filter_area = "filter_area";
            string f_action = "action";
            string f_page = "page";

            // Field values
            string v_filternrp = AHMIDEAFunctions.ContextGetData(context, f_filter_nrp);
            string v_filterarea = AHMIDEAFunctions.ContextGetData(context, f_filter_area);
            string v_action = AHMIDEAFunctions.ContextPostData(context, f_action);
            int v_page = 1;
            int.TryParse(AHMIDEAFunctions.ContextPostData(context, f_page), out v_page);
            if (v_page <= 0) v_page = 1;

            if (string.IsNullOrEmpty(v_action) || (v_action.ToLower() == "goto_page") || (v_action.ToLower() == "sort") || (v_action.ToLower() == "filter"))
            {
                context.Response.ContentType = "text/html";
                context.Response.Write(generateListData(context, v_page, v_filternrp, v_filterarea));
            }
        }
        else {
            axresp = AHMIDEAFunctions.NotAuthorizedResponse();
            context.Response.Write(axresp);
        }
        
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

    string generateListData(HttpContext context, int v_page, string filternrp, string filterarea )
    {
        // Set default values
        int maxDisplayed = int.Parse(ConfigurationManager.AppSettings["_CONFIG_MAX_DISPLAYED_ROWS"]);
        int totalData = 0;
        
        // get mode
        string vmode = AHMIDEAFunctions.ContextGetData(context, "mode");
        
        // get sort data
        string vsortby = AHMIDEAFunctions.ContextGetData(context, "sortby");
        string vsortdir = AHMIDEAFunctions.ContextGetData(context, "sortdir");

        // Define Table Fields
        List<ResponsiveTableField> fields = new List<ResponsiveTableField>();
        fields.Add(new ResponsiveTableField
        {
            FieldTitle = "NRP",
            FieldName = "vnrp",
            Priority = ResponsiveTableField.DisplayPriority.Priority2,
            Width = "5%",
            Sortable = true
        });
        fields.Add(new ResponsiveTableField
        {
            FieldTitle = "Nama",
            FieldName = "vnama",
            Priority = ResponsiveTableField.DisplayPriority.Priority1,
            IsMainField = true,
            Sortable = true
        });
        fields.Add(new ResponsiveTableField
        {
            FieldTitle = "Gol",
            FieldName = "ngolongan",
            Priority = ResponsiveTableField.DisplayPriority.Priority2,
            Width = "5%",
            Sortable = true
        });
        fields.Add(new ResponsiveTableField
        {
            FieldTitle = "Area",
            FieldName = "varea",
            Priority = ResponsiveTableField.DisplayPriority.Priority2,
            Width = "5%",
            Sortable = true
        });
        fields.Add(new ResponsiveTableField
        {
            FieldTitle = "Direktorat",
            FieldName = "vdirektorat",
            Priority = ResponsiveTableField.DisplayPriority.Priority2,
            Width = "5%",
            DetailViewOnly = true
        });
        fields.Add(new ResponsiveTableField
        {
            FieldTitle = "Divisi",
            FieldName = "vdivisi",
            Priority = ResponsiveTableField.DisplayPriority.Priority2,
            DetailViewOnly = true
        });
        fields.Add(new ResponsiveTableField
        {
            FieldTitle = "Departemen",
            FieldName = "vdepartment",
            Priority = ResponsiveTableField.DisplayPriority.Priority3,
            Sortable = true
        });
        fields.Add(new ResponsiveTableField
        {
            FieldTitle = "Sub Departemen",
            FieldName = "vsubdept",
            Priority = ResponsiveTableField.DisplayPriority.Priority4,
            Sortable = true
        });
        fields.Add(new ResponsiveTableField
        {
            FieldTitle = "Seksi",
            FieldName = "vsection",
            Priority = ResponsiveTableField.DisplayPriority.Priority4,
            Sortable = true
        });

        // Generate Default Data
        List<ResponsiveTableRow> tblData = new List<ResponsiveTableRow>();
        ResponsiveTableRow _row;
        List<ResponsiveTableItem> _items;
        using (DC_AHMHRIRSDataContext dc = new DC_AHMHRIRSDataContext())
        {
            var members = (from t in dc.AHMHRIRS_HDREMPs
                           join y in dc.AHMHRIRS_HDRAREAs on t.NAREA equals y.vareaid into data
                           from item in data.DefaultIfEmpty()
                           select new
                           {
                               VNRP = t.VNRP,
                               VNAMA = t.VNAMA,
                               NGOLONGAN = t.NGOLONGAN,
                               VDIREKTORAT = t.VDIREKTORAT,
                               VDEPARTMENT = t.VDEPARTMENT,
                               VDIVISI = t.VDIVISI,
                               VSUBDEPT = t.VSUBDEPT,
                               VSECTION = t.VSECTION,
                               VAREA = item==null? "":item.vareadesc,
                               NAREA = t.NAREA
                           });
            
            // check filter
            if (!string.IsNullOrEmpty(filternrp))
            {
                members = members.Where(t => t.VNRP.ToLower().Contains(filternrp) || t.VNAMA.ToLower().Contains(filternrp) || t.VDIREKTORAT.ToLower().Contains(filternrp) || t.VDEPARTMENT.ToLower().Contains(filternrp) || t.VDIVISI.ToLower().Contains(filternrp) || t.VSUBDEPT.ToLower().Contains(filternrp) || t.VSECTION.ToLower().Contains(filternrp) );
            }
            if (!string.IsNullOrEmpty(filterarea))
            {
                decimal darea = 0;
                decimal.TryParse(filterarea, out darea);
                members = members.Where(t => t.NAREA == darea);
            }
            // Untuk Ide Proposal, tampilkan semua karyawan golongan 1 s/d 3
            if (!string.IsNullOrEmpty(vmode) && (vmode.ToLower()=="ip")) {
                members = members.Where(t => t.NGOLONGAN < 4);
            }
            
            // check order
            if (!string.IsNullOrEmpty(vsortby)) {
                switch (vsortby)
                {
                    case "vnrp":
                        members = !string.IsNullOrEmpty(vsortdir) && (vsortdir == "asc") ? members.OrderBy(i => i.VNRP) : members.OrderByDescending(i => i.VNRP);
                        break;
                    case "vnama":
                        members = !string.IsNullOrEmpty(vsortdir) && (vsortdir == "asc") ? members.OrderBy(i => i.VNAMA) : members.OrderByDescending(i => i.VNAMA);
                        break;
                    case "ngolongan":
                        members = !string.IsNullOrEmpty(vsortdir) && (vsortdir == "asc") ? members.OrderBy(i => i.NGOLONGAN) : members.OrderByDescending(i => i.NGOLONGAN);
                        break;
                    case "varea":
                        members = !string.IsNullOrEmpty(vsortdir) && (vsortdir == "asc") ? members.OrderBy(i => i.VAREA) : members.OrderByDescending(i => i.VAREA);
                        break;
                    case "vdepartment":
                        members = !string.IsNullOrEmpty(vsortdir) && (vsortdir == "asc") ? members.OrderBy(i => i.VDEPARTMENT) : members.OrderByDescending(i => i.VDEPARTMENT);
                        break;
                    case "vsubdept":
                        members = !string.IsNullOrEmpty(vsortdir) && (vsortdir == "asc") ? members.OrderBy(i => i.VSUBDEPT) : members.OrderByDescending(i => i.VSUBDEPT);
                        break;
                    case "vsection":
                        members = !string.IsNullOrEmpty(vsortdir) && (vsortdir == "asc") ? members.OrderBy(i => i.VSUBDEPT) : members.OrderByDescending(i => i.VSUBDEPT);
                        break;
                }
                    
            }

            totalData = members.Count();
            if ((int)Math.Ceiling((double)totalData / maxDisplayed) < v_page) v_page = 1;

            var queryResultPage = members.Skip(maxDisplayed * (v_page - 1)).Take(maxDisplayed);
            foreach (var ttitem in queryResultPage)
            {
                _row = new ResponsiveTableRow();
                _items = new List<ResponsiveTableItem>();
                _items.Add(new ResponsiveTableItem { Key = "vnrp", Value = ttitem.VNRP });
                _items.Add(new ResponsiveTableItem { Key = "vnama", Value = ttitem.VNAMA });
                _items.Add(new ResponsiveTableItem { Key = "ngolongan", Value = ttitem.NGOLONGAN.ToString() });
                _items.Add(new ResponsiveTableItem { Key = "vdivisi", Value = ttitem.VDIVISI });
                _items.Add(new ResponsiveTableItem { Key = "varea", Value = ttitem.VAREA });
                _items.Add(new ResponsiveTableItem { Key = "vdirektorat", Value = ttitem.VDIREKTORAT });
                _items.Add(new ResponsiveTableItem { Key = "vdepartment", Value = ttitem.VDEPARTMENT });
                _items.Add(new ResponsiveTableItem { Key = "vsubdept", Value = ttitem.VSUBDEPT });
                _items.Add(new ResponsiveTableItem { Key = "vsection", Value = ttitem.VSECTION });
                _row.AddRow(_items);
                tblData.Add(_row);
            }
        }

        // Generate Table
        ResponsiveTable respTable = new ResponsiveTable(tblData);
        respTable.Fields = fields;
        respTable.Page = v_page;
        respTable.TotalData = totalData;
        if (!string.IsNullOrEmpty(vsortby)) respTable.SortBy = vsortby;
        if (!string.IsNullOrEmpty(vsortdir)) respTable.SortDirection = vsortdir == "desc" ? ResponsiveTable.Sort.Descending : ResponsiveTable.Sort.Ascending;

        respTable.SelectAction = "ahmhrirsl01_select(this)";
        
        return respTable.RenderTable();
    }
}